using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Web;
using System.Web.UI;
using System.Web.UI.Design;
using System.Web.UI.WebControls;
using PHSRAG.Security.Enumerations;
using PHSRAG.Insight21.Context;

namespace PHSRAG.Insight21.InsightWebControls
{
	/// <summary>
	/// Summary description for PurchasingSubNavigation.
	/// </summary>
	/// <summary>
	/// Summary description for PurchasingSubNavigation.
	/// </summary>
	[ToolboxData("<{0}:PurchasingSubNavigation runat=server />")]
	[Designer(typeof(PurchasingSubNavigationDesigner), typeof(IDesigner))]
	public class PurchasingSubNavigation : SubNavigation
	{
		#region Constants
		private const string	SelectedPurchasingSubNavigationLinkKey = "SelectedPurchasingubNavigationLink_3A31E802-4197-47fa-8D65-E771A4A40D77";
		#endregion

		#region Instance Variables
		private LinkButton poapSummaryLink;
		private LinkButton userOrderSummaryLink;
		private LinkButton reconcileLink;
		private	Policy.ICachePolicy	cachePolicy;
		private UserContext			userContext;
		#endregion

		#region Private Properties
		/// <summary>
		/// Return true if the control is in design mode.
		/// </summary>
		private static bool IsDesignMode
		{
			get { return HttpContext.Current == null; }
		}
		#endregion

		#region Constants (Public)
		/// <summary></summary>
		public const string POAPSummaryCommand = "PO/AP Summary";
		/// <summary></summary>
		public const string UserOrderSummaryCommand = "User Order Summary";
		/// <summary></summary>
		public const string ReconcileCommand = "Reconcile";
		#endregion

		/// <summary>
		/// Set the selected summary link so that on load, the appropriate tab may be shown highlighted.
		/// This property is invoked by pages while vectoring to other pages which host this navigation control.
		/// </summary>
		public string SelectedSummaryLink
		{
			set { Page.Session[SelectedPurchasingSubNavigationLinkKey] = value; }
		}

		#region Implementations for virtual methods from Base Class SubNavigation
		/// <summary>
		/// Handler for the event raised when the page is being initialized.
		/// Retrieve the cache policy and page cache policy in force.
		/// </summary>
		/// <param name="e">Argument associated with the event</param>
		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);
			if (!IsDesignMode)
			{
				cachePolicy = (Policy.ICachePolicy)Page.Session[Policy.Names.CachePolicyKey];
				userContext = (UserContext)cachePolicy.GetCachedObject(UserContext.CacheKey);
			}
		}

		/// <summary>
		/// Create all controls in the PurchasingSubNavigation.
		/// </summary>
		protected override void CreateControls()
		{
			Controls.Clear();

			poapSummaryLink = new LinkButton();
			poapSummaryLink.CommandName = poapSummaryLink.Text = "PO/AP Summary";
			poapSummaryLink.CommandArgument = Utility.Settings.GetKeyValue("SubNavigation.Purchasing.POAPSummaryURL", "");

			if (IsDesignMode || (userContext.ModuleRoleBitmask & (int)ModuleRoleBit.ManageUserOrders) != 0)
			{
				userOrderSummaryLink = new LinkButton();
				userOrderSummaryLink.CommandName = userOrderSummaryLink.Text = "User Order Summary";
				userOrderSummaryLink.CommandArgument = Utility.Settings.GetKeyValue("SubNavigation.Purchasing.UserOrderSummaryURL", "");

				reconcileLink = new LinkButton();
				reconcileLink.CommandName = reconcileLink.Text = "Reconcile";
				reconcileLink.CommandArgument = Utility.Settings.GetKeyValue("SubNavigation.Purchasing.ReconcileURL", "");
			}
		}

		/// <summary>
		/// The selected link cell is given a prefix and suffix image as adornments. In addition the background of the
		/// cell is of a different style.
		/// </summary>
		private void CreateSelectedLinkAdornments(TableRow row)
		{
			LinkButton selectedLink = GetSelectedLink();
			if (selectedLink != null)
			{
				foreach (TableCell cell in row.Cells)
				{
					if ((cell.Controls.Count == 1) && (cell.Controls[0] == selectedLink))
					{
						cell.CssClass = "SubModuleNavigationSelectedSummary";

						TableCell adornmentCell = new TableCell();
						adornmentCell.CssClass = "SubModuleNavigationSelectedSummaryAdornment";
						row.Cells.AddAt(row.Cells.GetCellIndex(cell), adornmentCell);

						const string prefixImage = "../Images/Row2_Left.gif";
						System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(Context.Server.MapPath(prefixImage));
						Image image = new Image();
						image.ImageUrl = prefixImage;
						adornmentCell.Width = bitmap.Width;
						adornmentCell.Controls.Add(image);

						adornmentCell = new TableCell();
						adornmentCell.CssClass = "SubModuleNavigationSelectedSummaryAdornment";
						row.Cells.AddAt(row.Cells.GetCellIndex(cell) + 1, adornmentCell);

						const string suffixImage = "../Images/Row2_Right.gif";
						bitmap = new System.Drawing.Bitmap(Context.Server.MapPath(suffixImage));
						image = new System.Web.UI.WebControls.Image();
						image.ImageUrl = suffixImage;
						adornmentCell.Width = bitmap.Width;
						adornmentCell.Controls.Add(image);

						break;
					}
				}
			}
		}

		/// <summary>
		/// Setup the control tree to reflect the actual (and exact) layout of the controls within the PurchasingSubNavigation.
		/// </summary>
		protected override void GenerateControlTree()
		{
			Table table = new Table();
			Controls.Add(table);
			table.CssClass = "SubModuleSummaryNavigation";

			TableRow row = new TableRow();
			table.Rows.Add(row);

			TableCell cell = new TableCell();
			row.Cells.Add(cell);
			cell.CssClass = "SubModuleSummaryNavigationFillerCell";
			cell.Controls.Add(new LiteralControl("&nbsp;"));

			cell = new TableCell();
			row.Cells.Add(cell);

			table = new Table();
			cell.Controls.Add(table);

			row = new TableRow();
			table.Rows.Add(row);

			cell = new TableCell();
			row.Cells.Add(cell);

			cell = new TableCell();
			row.Cells.Add(cell);
			cell.Controls.Add(poapSummaryLink);

			if (userOrderSummaryLink != null) 
			{
				cell = new TableCell();
				row.Cells.Add(cell);
				cell.Controls.Add(userOrderSummaryLink);
			}

			if (reconcileLink != null) 
			{
				cell = new TableCell();
				row.Cells.Add(cell);
				cell.Controls.Add(reconcileLink);
			}

			CreateSelectedLinkAdornments(row);
		}

		/// <summary>
		/// Retrieve the name of the link that was last clicked on.
		/// </summary>
		/// <returns>Reference to the link button corresponding to the clicked link</returns>
		protected override LinkButton GetSelectedLink()
		{
			// Any time a link is clicked on, the OnBubbleEvent handler stores the name of the link in the session.
			// Retrieve that name and return the corresponding link button reference.

			object o = Page.Session[SelectedPurchasingSubNavigationLinkKey];
			if (o != null)
			{
				string commandName = (string)o;
				if (commandName == poapSummaryLink.CommandName)
					return poapSummaryLink;
				if (commandName == userOrderSummaryLink.CommandName)
					return userOrderSummaryLink;
				if (commandName == reconcileLink.CommandName)
					return reconcileLink;
			}
			return poapSummaryLink;
		}
		
		/// <summary>
		/// Gets the key used to the store the SelectedSubNavigationLink information
		/// in any hash table. 
		/// </summary>
		protected override string SelectedSubNavigationLinkKey
		{
			get 
			{
				return SelectedPurchasingSubNavigationLinkKey;
			}
		}

		#region Event Handlers
		/// <summary>
		/// Disable view state.
		/// </summary>
		/// <param name="e">Argument associated with the load event.</param>
		protected override void OnLoad(EventArgs e)
		{
			EnableViewState = false;
			base.OnLoad (e);	
		}
		#endregion

		#endregion
	}

	#region Class PurchasingSubNavigationDesigner
	/// <summary>
	/// Designer associated with the control.
	/// </summary>
	public class PurchasingSubNavigationDesigner : ControlDesigner
	{
		#region Overrides
		/// <summary>
		/// Force the control to create its child controls for display at design time.
		/// </summary>
		/// <returns>HTML representation of the control</returns>
		public override string GetDesignTimeHtml()
		{
			((PurchasingSubNavigation)Component).Design();
			return base.GetDesignTimeHtml();
		}
		#endregion
	}
	#endregion
}
